const { query } = require('express')
const express=require('express')
const connection=require('../../util/request')
const router=express.Router()
let data={}
//查询所有用户
router.get('/user/userInfo',(req,resp,next)=>{
  let Num=req.query.pageSize*(req.query.pageNum-1)
  let sql='SELECT role_u.uid,username,`password`,status_u,realname,createTIme,editTime,introduction,avatar,role.role_id,role_remark,sex,phone,email,delStatus FROM `user`,role_u,role WHERE `user`.uid=role_u.uid AND role_u.role_id=role.role_id'
  let querySql=` AND username like '%${req.query.queryWhere}%'`
  let limitSql=` LIMIT ${Num},${req.query.pageSize}`
  connection.query(sql+querySql,(err,result,faild)=>{
    if(err)next(err)
    data.total=result.length || 0
    connection.query(sql+querySql+limitSql,(err,result,faild)=>{
      if(err)next(err)
      data.code=200
      data.message=''
      result.forEach(item=>{
        item.avatar=[item.avatar]
      })
      data.result=result || []
      resp.status(200).json(data)
    })
  })
})
//修改用户
router.post('/user/updateUser',(req,resp,next)=>{
  if(req.body.username=='admin'){
    data.code=409
    data.message='admin账号禁止修改'
    data.result=[]
    resp.status(200).json(data)
  }else{
    let sql=`UPDATE `+`user`+` SET `+`password`+`='${req.body.password}',sex='${req.body.sex}',realname='${req.body.realname}',status_u='${req.body.status_u}',editTime=NOW(),introduction='${req.body.introduction}',avatar='${req.body.avatar[0]}',
    phone='${req.body.phone}',email='${req.body.email}',delStatus=${req.body.delStatus} WHERE uid=${req.body.uid}`
    connection.query(sql,(err,result,faild)=>{
      if(err) next(err)
      sql=`UPDATE role_u SET role_id=${req.body.role_id} WHERE uid=${req.body.uid}`
      connection.query(sql,(err,result,faild)=>{
        if(err) next(err)
        data.code=201
        data.message='修改成功'
        data.result=[]
        resp.status(200).json(data)
      })
    })
  }
})
//添加用户
router.put('/user/addUser',(req,resp,next)=>{
  let sql=`SELECT username FROM `+`user`+` WHERE username='${req.body.username}'`
  connection.query(sql,(err,result,faild)=>{
    if(err)next(err)
    if(result.length>0){
      data.code=409
      data.message='添加失败,用户名重复'
      data.result=[]
      resp.status(200).json(data)
    }else{
       sql=`INSERT INTO `+`user`+`(username,`+`password`+`,realname,sex,status_u,email,createTIme,introduction,phone,avatar) 	VALUES('${req.body.username}','${req.body.password}','${req.body.realname}','${req.body.sex}','${req.body.status_u}','${req.body.email}',NOW(),'${req.body.introduction}','${req.body.phone}','${req.body.avatar[0]}')`
      connection.query(sql,(err,result,faild)=>{
        if(err)next(err)
        let uid=result.insertId
        let sql=`INSERT INTO role_u(role_id,uid) VALUES(${req.body.role_id},${uid})`
        connection.query(sql,(err,result,faild)=>{
          data.code=201
          data.result=[]
          data.message='用户添加成功'
          resp.status(200).json(data)
        })
        //添加之后回显
      })
    }
  })
})

//获得用户权限
router.get('/user/role',(req,resp,next)=>{
  let sql=`SELECT role_id,role_name,role_remark FROM role`
  connection.query(sql,(err,result,faild)=>{
    if(err)next(err)
    data.code=200
    data.result=result
    resp.status(200).json(data)
  })
})
//根据用户ID获取用户信息
router.get('/user/getUserInfo',(req,resp,next)=>{
  let sql=`SELECT role_u.uid,username,`+`password`+`,status_u,realname,createTIme,editTime,introduction,avatar,role.role_id,role_remark,sex,phone,email,delStatus FROM `+`user`+`,role_u,role WHERE `+`user`+`.uid=role_u.uid AND role_u.role_id=role.role_id AND `+`user`+`.uid=${req.user.uid} `
  connection.query(sql,(err,result,faild)=>{
    if(err) next(err)
    if(!result){
      data.code=403
      data.message='非法登入！！！'
      data.result=[]
      resp.status(200).json(data)
    }else{
      
      data.code=200
      data.message=''
      result.forEach(item=>{
        item.avatar=[item.avatar]
      })
      data.result=result
      resp.status(200).json(data)
    } 
  })
})
module.exports=router